import { createStore } from 'vuex'

export default createStore({
  state: {
    userId: null,
    userAvatar: null,
    userInfo: null
  },
  getters: {
    isLoggedIn: state => !!state.userId,
    getUserId: state => state.userId,
    getUserAvatar: state => state.userAvatar
  },
  mutations: {
    SET_USER_ID(state, userId) {
      state.userId = userId
    },
    SET_USER_AVATAR(state, avatar) {
      state.userAvatar = avatar
    },
    SET_USER_INFO(state, userInfo) {
      state.userInfo = userInfo
    }
  },
  actions: {
    updateUserInfo({ commit }, userInfo) {
      commit('SET_USER_INFO', userInfo)
      commit('SET_USER_ID', userInfo.id)
      commit('SET_USER_AVATAR', userInfo.avatar)
    }
  }
})
